package astra.client.pages.supplier;

import astra.client.models.Client;
import astra.client.models.Supplier;
import astra.client.pages.Page;
import astra.client.utils.widgets.table.ClickableTextColumn;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.view.client.ListDataProvider;

/**
 * @author Kotelnikov Dmitry <a href="mailto:dimonnot@mail.ru"/>
 */
public class SupplierPage extends Page {
    interface SupplierPageUiBinder extends UiBinder<HTMLPanel, SupplierPage> {
    }

    private static SupplierPageUiBinder ourUiBinder = GWT.create(SupplierPageUiBinder.class);

    @UiField(provided = true)
    CellTable table;

    @UiField
    Button showAddDialog;

    @UiField
    FlowPanel actions;

    private ListDataProvider<Supplier> dataProvider;

    @UiConstructor
    public SupplierPage(String name) {
        super(name);

        table = new CellTable<Supplier>();

        table.addColumn(
            new TextColumn<Supplier>() {
                @Override
                public String getValue(Supplier object) {
                    return object.getName();
                }
            },
            "Название"
        );

        table.addColumn(
            new TextColumn<Supplier>() {
                @Override
                public String getValue(Supplier object) {
                    return object.getAddress();
                }
            },
            "Адрес"
        );

        table.addColumn(
            new TextColumn<Supplier>() {
                @Override
                public String getValue(Supplier object) {
                    return object.getContact();
                }
            },
            "Контактное лицо"
        );

        table.addColumn(
            new TextColumn<Supplier>() {
                @Override
                public String getValue(Supplier object) {
                    return object.getCompany();
                }
            },
            "Компания"
        );

        table.addColumn(
            new ClickableTextColumn<Supplier>() {
                @Override
                public String getValue(Supplier object) {
                    return "изменить";
                }

                @Override
                public void onBrowserEvent(Cell.Context context, Element elem, Supplier object, NativeEvent event) {
                    astra.getSupplierPopup().edit(object);
                }
            },
            ""
        );

        dataProvider = new ListDataProvider<Supplier>();
        dataProvider.addDataDisplay(table);


        initWidget(ourUiBinder.createAndBindUi(this));
    }

    public void refresh() {
        dataProvider.refresh();
    }

    @Override
    public void show() {
        dataProvider.setList(astra.getAstraData().getSuppliers());
        showActions(actions);
    }

    @UiHandler("showAddDialog")
    public void handleClick(ClickEvent event) {
        astra.getSupplierPopup().edit(null);
    }
}